import React from 'react';
import {Form, Switch} from "antd";
import BaseEntity from "../../type/entity";
import {FormInstance} from "antd/lib/form/Form";
interface IProps<T> {
    name: keyof T,
    lastName?: keyof T,
    label?: string,
    /**
     * 值改变时回调函数
     * @param checked 选择器的值
     * @form 表单实例
     */
    onChange?: (checked: boolean, form: FormInstance<T>) => void,
}
const InputSwitch = <T extends BaseEntity>(props: IProps<T>) => {
    const form = Form.useFormInstance<T>();
    const switchChange = (checked: boolean) => {
      if (props.onChange){
          props.onChange(checked, form);
      }
    };
    return (
        <Form.Item<T> name={props.name as any} label={props.label} required>
            <Switch onChange={switchChange}/>
        </Form.Item>
    );
};

export default InputSwitch;